const App = getApp()
Page({
	data: {
		userInfo: {
      avatarUrl: '../../assets/images/iconfont-plus-circle.png'
    },
		items: [
			{
        icon: '../../assets/images/iconfont-plus-circle.png',
        text: '幼儿园登记', 
        path: '/pages/register/register'
			}, 
      /*{
        icon: '../../assets/images/iconfont-plus-circle.png',
        text: '特殊人员儿童登记',
        path: '/pages/tsregister/tsregister'
      }, */
      {
        icon: '../../assets/images/iconfont-help.png',
        text: '查看登记结果',
        path: '/pages/bmlist/bmlist'
      }, 
      {
        icon: '../../images/iconfont-order.png',
        text: '公示公告',
        path: '/pages/gsgg/gsgg'
      }, 
      {
        icon: '../../assets/images/iconfont-about.png',
        text: '联系我们', 
        path: '/pages/zxkf/zxkf'
      } 
		],
    grzx: App.myUlife.ulifeUrl + '/wx/grzx.jpg',
    ulifeUrl: App.myUlife.ulifeUrl
	},

  onLoad: function() {
    var that = this;
    var openid = wx.getStorageSync('openid');
 
    if (openid == "") {
      wx.redirectTo({
        url: '../../pages/index/index'
      })
    } else { 
      var userInfo= { 
        city: wx.getStorageSync('city'),
        province: wx.getStorageSync('province'),
        gender: wx.getStorageSync('gender'),
        nickName: wx.getStorageSync('nickName'),
        avatarUrl: wx.getStorageSync('avatarUrl'),
        openid : wx.getStorageSync('openid')
      }
      that.setData({
        userInfo: userInfo
      });
    } 
},

	navigateTo(e) {
		const path = e.currentTarget.dataset.path
			wx.navigateTo({
        url: path,
      })
    },
	
    bindtap(e) {
      const path = e.currentTarget.dataset.path
      wx.navigateTo({
        url: path,
      })
    },
    //退出登录
    exitlogin: function(){
      
      wx.redirectTo({
        url: '../../pages/index/index'
      })
    },

    bindGetUserInfo(){
      var that = this;
      if (wx.getSetting) {
        wx.getSetting({
          success(res) {
            if (!res.authSetting['scope.userInfo']) {
              wx.authorize({
                scope: 'scope.userInfo',
                success() {
                  // 登录
                  that.wxlogin();
                }
              })
            } else {
              // 登录
              that.wxlogin();
            }
          }
        })
      } else {
        //不兼容处理
        // 登录
        that.wxlogin();
      }
    },

      // 登录start
  wxlogin: function () {
    var that = this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        that.code = res.code;
        that.getwxuserinfo();
      }
    })
  },


   // 发送 res.code 到后台换取 openId, sessionKey, unionId
   getwxuserinfo: function () {
    var that = this;
    wx.getUserInfo({
      success: res => {
        wx.hideLoading(res.userInfo);
        //加入缓存
        wx.setStorageSync('city', res.userInfo.city);
        wx.setStorageSync('province', res.userInfo.province);
        wx.setStorageSync('gender', res.userInfo.gender);
        wx.setStorageSync('nickName', res.userInfo.nickName);
        wx.setStorageSync('avatarUrl', res.userInfo.avatarUrl);

        var userInfo= { 
          city: res.userInfo.city,
          province: res.userInfo.province,
          gender: res.userInfo.gender,
          nickName: res.userInfo.nickName,
          avatarUrl: res.userInfo.avatarUrl,
        }
        that.setData({
          userInfo: userInfo
        });
        console.log(userInfo)
        // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        if (that.userInfoReadyCallback) {
          that.userInfoReadyCallback(res)
        }
      }
    })

 
    wx.request({
      url: App.myUlife.ulifeUrl + '/wechart/getwxuserinfo.html',//请求地址
      data: {
        code: that.code
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var openid = res.data.openid;
        wx.setStorageSync('openid', openid);//加入缓存

        if (that.data.sfkqbm) {
          wx.setStorageSync('sfkqbm', that.data.sfkqbm);//加入缓存
        }

        if (that.data.bmjzrq) {
          wx.setStorageSync('bmjzrq', that.data.bmjzrq);
        }
        if (that.data.rxstart) {
          wx.setStorageSync('rxstart', that.data.rxstart);
        }
        if (that.data.rxend) {
          wx.setStorageSync('rxend', that.data.rxend);
        }
      }
    })
  }



})